package cp.server.app;

import cp.server.common.Comment;
import cp.server.common.HotComment;
import cp.server.common.Keyword;
import cp.server.common.NewsType;
import cp.server.common.SystemProperty;
import cp.server.util.FileUtils;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import com.google.gson.Gson;

public class CollectData
{
    private static Map<String, List<Comment>> PageCmts;
    private static List<HotComment> RealTimeHotCmts;
    private static Map<NewsType, List<HotComment>> HotCmts;
    private static List<List<HotComment>> KeyWordHotCmts;
    private static List<Keyword> KeyWords;

    public String getRealTimeHotCmts()
    {
        Gson gson = new Gson();
        
        try
        {
            RealTimeHotCmts = (List<HotComment>) FileUtils
                    .readObjectFromFile(SystemProperty.RealTimeHotCommentFileName);
        }
        catch (FileNotFoundException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch (ClassNotFoundException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        return gson.toJson(RealTimeHotCmts);
    }
    
    public String getHotCmtsByType(NewsType type)
    {
        Gson gson = new Gson();
        
        try
        {
            HotCmts = (Map<NewsType, List<HotComment>>) FileUtils
                    .readObjectFromFile(SystemProperty.HotCommentFileName);
        }
        catch (FileNotFoundException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch (ClassNotFoundException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        return gson.toJson(HotCmts.get(type));
    }    
    
    public String getCmtByKeyword()
    {
        Gson gson = new Gson();
        
        try
        {
            KeyWords = (List<Keyword>) FileUtils
                    .readObjectFromFile(SystemProperty.KeywordListFileName);
        }
        catch (FileNotFoundException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch (ClassNotFoundException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        return gson.toJson(KeyWords);
    }    
    
    public static void main(String args[]) throws FileNotFoundException,
            ClassNotFoundException, IOException
    {
        CollectData c = new CollectData();
        c.getCmtByKeyword();
        // FileUtils.readObjectFromFile((SystemProperty.HotListFileName + "_"
        // + pageDate + "_" + pageId, hotList);
        // FileUtils.readObjectFromFile(SystemProperty.LatestListFileName
        // + "_" + pageDate + "_" + pageId, latestList);

        KeyWords = (List<Keyword>) FileUtils
                .readObjectFromFile(SystemProperty.KeywordListFileName);
        // KeyWordHotCmts = (List<List<HotComment>>) FileUtils
        // .readObjectFromFile(SystemProperty.KeywordCommentListFileName);
        RealTimeHotCmts = (List<HotComment>) FileUtils
                .readObjectFromFile(SystemProperty.RealTimeHotCommentFileName);

//        for (HotComment hc : RealTimeHotCmts)
//        {
//            System.out.println(hc.getPageTitle());
//            System.out.println(hc.getTime() + " " + hc.getAgree()
//                    + hc.getContent());
//        }

         for (Keyword k : KeyWords)
         {
         System.out
         .println("*****************************************************************");
         System.out.println(k.getKeyword() + " " + k.getCount());
        
         for (int i = 0; i < k.getPageList().size(); i++)
         {
         String pageId = k.getPageList().get(i).getId();
         String pageDate = k.getPageList().get(i).getTime().toString().substring(0,10);
         System.out.println("---------------------" + pageId);
        
         List<Comment> pageHotCmtList = (List<Comment>) FileUtils
         .readObjectFromFile(SystemProperty.HotListFileName
         + "_" + pageDate + "_" + pageId);
         List<Comment> pageLatestCmtList = (List<Comment>) FileUtils
         .readObjectFromFile(SystemProperty.LatestListFileName
         + "_" + pageDate + "_" + pageId);
        
         if (pageHotCmtList == null)
         {
         System.out.println("shit happens");
         }
         else
         {
         for (int j = 0; j < 8; j++)
         {
         System.out.println(pageHotCmtList.get(j).getContent()
         + " " + pageHotCmtList.get(j).getAgree());
         }
         }
        
         // if (pageLatestCmtList == null)
         // {
         // System.out.println("shit happens");
         // }
         // else
         // {
         // for (int j = 0; j < 10; j++)
         // {
         // System.out.println(pageLatestCmtList.get(j).getContent()
         // + " " + pageLatestCmtList.get(j).getAgree());
         // }
         // }
        
         }
        
         System.out
         .println("*****************************************************************");
         }

        // for (List<HotComment> l : KeyWordHotCmts)
        // {
        // if (l != null && l.size() != 0)
        // System.out.println(l.get(0).getContent() + " "
        // + l.get(0).getAgree());
        // else
        // System.out.println("shit happens");
        // }
        System.out.println("heyhey");
    }

}
